#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#include "list.h"

void build_huffman_tree(struct list * l){
	
}

void getcode(struct list *head){
	stack s;
	
}

void huffman_code(const char* str){
	//first caculate weight
	struct list head;
	head.c='$';
	head.next=NULL;
	struct list* last=&head;
	int len=(int)strlen(str);
	int i;
	struct list* st=NULL;
	
	for(i=0;i<len;i++){
		struct list* p=find(&head,str[i]);
		if(p==NULL){
			struct list* n=(struct list*)malloc(sizeof(struct list));
			last->next=n;
			n->c=str[i];
			last=n;
			n->next=NULL;
			n->weight=1;
		}else{
			p->weight++;
		}
	}
	
	printList(&head);
	struct list * h=sort_list(&head);
	printf("\nafter sort:\n");
	printList(h);
	printf("after reverse \n");
	h=reverse_list(h);
	printList(h);
	
	build_huffman_tree(h);
	
	getcode(h);
}



int main(){
	const char* str="this is an example of a huffman tree";
	huffman_code(str);
	
	return 0;
}